﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ClockwiseClient
{
    public partial class FormKeyboard : Form
    {
        delegate void UpdateListCallback(List<string> lst);
        FormMain formMain;

        public FormKeyboard(FormMain main)
        {
            InitializeComponent();
            formMain = main;
        }

        public void Reset(char key)
        {
            textBox1.Text = key.ToString();
            textBox1.Select(1,0);
            UpdateList();
        }

        private void FormKeyboard_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                if (textBox1.Text.Length > 0)
                {
                    if (textBox1.Text.Length == 1)
                    {
                        Visible = false;
                        return;
                    }
                    textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                    textBox1.SelectionStart = textBox1.Text.Length;
                    UpdateList();
                }
            }
            else if(e.KeyCode == Keys.Enter)
            {
                String text = (String) listBox1.SelectedItem;
                Visible = false;
                if(text != null) formMain.SetKeyboardCommand(text);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                textBox1.Text = "";
                Visible = false;
            }
        }

        private void FormKeyboard_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox1.Text.Length < 6)
            {
                if (textBox1.Text.Length == 0 && e.KeyChar == '.' || textBox1.Text.Length < 6 && (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar >= 'A' && e.KeyChar <= 'Z' || e.KeyChar >= 'a' && e.KeyChar <= 'z'))
                {
                    textBox1.Text += e.KeyChar.ToString();
                    textBox1.SelectionStart = textBox1.Text.Length;
                    UpdateList();
                }
            }
            e.Handled = true;
        }

        private void FormKeyboard_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Visible = false;
        }

        private void FormKeyboard_Load(object sender, EventArgs e)
        {
            textBox1.Left = 6;
            textBox1.Top = 6;
            listBox1.Size = new Size(Size.Width - 18, Size.Height - textBox1.Size.Height - 36);
            listBox1.Top = textBox1.Bottom + 6;
            listBox1.Left = 6;
            listBox1.SelectionMode = SelectionMode.One;
        }

        private void UpdateList()
        {
            if (listBox1.Items.Count > 0) listBox1.Items.Clear();
            if (textBox1.Text.Length == 0) return;
            if (textBox1.Text[0] >= '0' && textBox1.Text[0] <= '9' && textBox1.Text.Length <= 6 || textBox1.Text[0] == '.')
            {
                new Thread(LoadCodeName).Start(textBox1.Text);
            }
            else if (textBox1.Text[0] == '/' && textBox1.Text.Length == 1)
            {
                new Thread(LoadCodeName).Start("");
            }
        }

        private void UpdateListBoxFinish(List<string> lstCodeName)
        {
            if (listBox1.InvokeRequired)
            {
                while (listBox1.IsHandleCreated == false)
                {
                    if (listBox1.Disposing || listBox1.IsDisposed) return;
                }
                UpdateListCallback call = new UpdateListCallback(UpdateListBoxFinish);
                listBox1.Invoke(call, new object[] { lstCodeName });
            }
            else
            {
                foreach (string str in lstCodeName)
                {
                    listBox1.Items.Add(str);
                }
                listBox1.SelectedIndex = 0;
                listBox1.Focus();
            }
        }

        private void LoadCodeName(Object o)
        {
            String key = (string)o;
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            List<String> lstCodeName = classStockData.LoadCodeName(key);
            if (lstCodeName.Count > 0)
            {
                UpdateListBoxFinish(lstCodeName);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
